﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
///ImageUploadEditShow 的摘要说明
/// </summary>
public class ImageUploadEditShow:BaseEditShowField
{
	public ImageUploadEditShow()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public override IEnumerable<Control> GetThisTypeControl(ConfigColumns cCol, string value, bool isEdit)
    {
        HyperLink img = new HyperLink();
        img.ID = cCol.ColumnName + "_img";
        img.Target = "_blank";
        img.Height = new Unit(40);
        img.Width = new Unit(100);
        
        if (!string.IsNullOrEmpty(cCol.EditShowImgHeight))
        {
            img.Height = new Unit(cCol.EditShowImgHeight);
        }
        if (!string.IsNullOrEmpty(cCol.EditShowImgWidth))
        {
            img.Width = new Unit(cCol.EditShowImgWidth);
        }
        if (isEdit)
        {
            //img.ImageUrl = "~/uploads/" + value;
            img.ImageUrl = "~/tools/imgshowcut.ashx?p=../uploads/" + value + "&s=t&w=" + cCol.EditShowImgWidth + "&h=" + cCol.EditShowImgHeight;
            img.NavigateUrl = "~/uploads/" + value;
            
        }
        HiddenField hide = new HiddenField();
        hide.ID = cCol.ColumnName+"_EditShow";
        if (isEdit) hide.Value = value;
        AjaxFileUpload uploader = new AjaxFileUpload();
        uploader.AllowExt = cCol.EditShowAllowUploadExt;
        uploader.ID = cCol.ColumnName + "_uploader";
        uploader.StaticScript = false;

        uploader.OnSuccessScript = @"function (data, status)
                            {
                                if (typeof (data.error) != 'undefined')
                                {
                                    if (data.error != '')
                                    {
                                        alert(data.error);
                                    } 
                                    else
                                    {
                                        {dododo}
                                    }
                                }
                            }";
        string successDo = "$(\"#"+img.ID+" img\").attr(\"src\",'../uploads/'+data.msg).width('"+img.Width+"').height('"+img.Height+"');$(\"#" + img.ID + "\").attr(\"href\",'../uploads/'+data.msg).show();$(\"#" + hide.ID + "\").val(data.msg);";
        if (!isEdit)
        {
            successDo = "$(\"#" + img.ID + "\").append(\"<img border=0 />\");$(\"#" + img.ID + " img\").attr(\"src\",'../uploads/'+data.msg).width('" + img.Width + "').height('" + img.Height + "');$(\"#" + img.ID + "\").attr(\"href\",'../uploads/'+data.msg).show();$(\"#" + hide.ID + "\").val(data.msg);";
        }
        uploader.OnSuccessScript = uploader.OnSuccessScript.Replace("{dododo}", successDo);
                
        string script = "$(function () {";
        if (!isEdit)
        {
            script += "$(\"#" + img.ID + "\").hide()";
        }
        else
        {
            if (string.IsNullOrEmpty(value))
            {
                script += "$(\"#" + img.ID + "\").hide()";
            }
            else
            {
                if (string.IsNullOrEmpty(cCol.EditShowImgWidth))
                {
                    script += "$(\"#" + img.ID + " img\").width('100');";
                }
                else
                {
                    script += "$(\"#" + img.ID + " img\").width('" + cCol.EditShowImgWidth + "');";
                }
                if (string.IsNullOrEmpty(cCol.EditShowImgHeight))
                {
                    script += "$(\"#" + img.ID + " img\").height('40');";
                }
                else
                {
                    script += "$(\"#" + img.ID + " img\").height('" + cCol.EditShowImgHeight + "');";
                }
            }
        }
        script += "})";

        Helper.WriteScriptAtTop(script, cCol.ColumnName + "_SetupScript");

        yield return img;
        yield return hide;
        yield return uploader;
    }
}
